package artid.com.arttogo.activity;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView.HitTestResult;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.AddressListAdapter;
import artid.com.arttogo.adapter.AuctionPastAuction_Adapter;
import artid.com.arttogo.bean.AddressList;
import artid.com.arttogo.bean.AuctionAuctionListParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 *  选择地址
 * 
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class ChoiceAddressActivity extends BaseActivity implements OnClickListener {
	ListView mlistAddress;
	TextView mBtnadd;
	AddressListAdapter mAdapter;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page = 1;
	private LinearLayout noDataLL;
	private int dataNum;//数据总条数
	private int requestDataNum=20;//请求数据数量
	private Boolean isLoad=true;//是否加载数据（默认可以加载）
	private List<AddressList> list=new ArrayList<>();
	private List<AddressList> more_list=new ArrayList<>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choiceaddress);
		mContext = ChoiceAddressActivity.this;
		mBack = (ImageView) findViewById(R.id.back_btn);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("选择收货地址");
		mBtnadd = (TextView) findViewById(R.id.ChoiceAddress_address_buy);
		mBtnadd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ChoiceAddressActivity.this, AddAddressActivity.class);
				startActivity(intent);
			}
		});
		noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
		mListView = (PullableListView) findViewById(R.id.ChoiceAddress_ListView);
		mListView.setOnItemClickListener(onItemClick);
//		mListView.setOnScrollListener(scrollListener);
		mListView.isCanPullUp=false;//关闭加载更多
		mPullLayout = (PullToRefreshLayout) findViewById(R.id.ChoiceAddress_refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 下拉刷新
				page = 1;
				isRefreshed = true;
				getData(true);
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 上拉加载
				page++;
				getData(false);
			}
		});
		//getData(true);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData(true);

	}

	/**
	 * Item点击事件
	 */
	OnItemClickListener onItemClick=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
								long arg3) {
			// TODO Auto-generated method stub

		}
	};

//	AbsListView.OnScrollListener scrollListener=new AbsListView.OnScrollListener() {
//		@Override
//		public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//		}
//
//		@Override
//		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//			if(more_list.size()!=dataNum && isLoad==true){
//				//-10是数据已经显示一半的时候
//				if(page==1 && firstVisibleItem==(requestDataNum-10)){
//					page++;
//					getData(false);
//				}else if(((page*requestDataNum)-10)==firstVisibleItem){
//					page++;
//					getData(false);
//				}
//			}
//		}
//	};



	private void getData(Boolean isLoadDialog){
		isLoad=false;
		if(isLoadDialog){
			VolleyBasic.showLoding(this);
		}
		VolleyBasic.getInstanceNoDialog(this).post(
				Cache.addressList,
				VolleyMap.getInstance().addressList(page+"", requestDataNum+""),
				AddressList.class, new VolleyCallback() {

					@SuppressWarnings({ "unchecked", "static-access" })
					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
						list= (List<AddressList>) vd.getList();
						isLoad=true;
						if(page==1) {
                            more_list.clear();
							try {
								JSONObject jsonObject = new JSONObject(vd.getJson());
								dataNum = jsonObject.getInt("num");
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
						if (isRefreshed == true) {
							more_list.clear();
							isRefreshed = false;
						}
						if (!list.isEmpty()) {
							more_list.addAll(list);
						}

						//这里是显示有数据和无数据的布局
						if(more_list!=null && more_list.size()!=0){
							noDataLL.setVisibility(View.GONE);
						}else{
							noDataLL.setVisibility(View.VISIBLE);
							return;
						}

						if (mAdapter == null) {
							mAdapter=new AddressListAdapter(mContext, more_list);
							mListView.setAdapter(mAdapter);

						}
						mAdapter.notifyDataSetChanged();
					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						isLoad=true;
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
						//这里是显示有数据和无数据的布局
						if(more_list!=null && more_list.size()!=0){
							noDataLL.setVisibility(View.GONE);
						}else{
							noDataLL.setVisibility(View.VISIBLE);
							return;
						}
					}
				});
	}


	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

}
